﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

public class ToModel<T> where T : new()
{
    /// <summary>
    /// 将DataSet轉化為List
    /// </summary>
    /// <param name="ds"></param>
    /// <returns></returns>
    public static List<T> DataSetToList(DataTable dt)
    {
        // 定義集合
        List<T> ts = new List<T>();
        try
        {
            // 獲得此模型的類型
            Type type = typeof(T);
            string tempName = "";
           
                
                foreach (DataRow dr in dt.Rows)
                {
                    T t = new T();
                    // 獲得此模型的公用變量
                    PropertyInfo[] propertys = t.GetType().GetProperties();

                    foreach (PropertyInfo proper in propertys)
                    {
                        tempName = proper.Name.ToUpper();

                        // 檢查DataTable是否包含此列
                        if (dt.Columns.Contains(tempName))
                        {
                            object value = dr[tempName];
                            if (value != DBNull.Value)
                            {
                                if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                    proper.SetValue(t, value, null);
                                else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                    proper.SetValue(t, value, null);
                                else
                                {
                                    if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                    {
                                        string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                        propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                        proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                    }
                                    else
                                        proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                                }
                            }
                        }
                    }
                    ts.Add(t);
                }
            
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return ts;
    }

    public static List<T> DataTableToList(DataTable dt)
    {
        // 定義集合
        List<T> ts = new List<T>();
        try
        {
            // 獲得此模型的類型
            Type type = typeof(T);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                // 獲得此模型的公用變量
                PropertyInfo[] propertys = t.GetType().GetProperties();

                foreach (PropertyInfo proper in propertys)
                {
                    tempName = proper.Name.ToUpper();

                    // 檢查DataTable是否包含此列
                    if (dt.Columns.Contains(tempName))
                    {
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                        {
                            if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                proper.SetValue(t, value, null);
                            else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                proper.SetValue(t, value, null);
                            else
                            {
                                if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                {
                                    string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                    propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                    proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                }
                                else
                                    proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                            }
                        }
                    }
                }
                ts.Add(t);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return ts;
    }


}